package com.sh.config;

import com.sh.service.impl.ResuserBizImpl;
import com.sh.web.filter.JwtFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

@Configuration
@EnableWebSecurity
public class WebSecurityConfig {
    private  ResuserBizImpl resuserBizImpl;
    @Autowired
    public void setResuserBizImpl(ResuserBizImpl resuserBizImpl) {
        this.resuserBizImpl = resuserBizImpl;
    }
    private JwtFilter jwtFilter;
    @Autowired
    public void setJwtFilter(JwtFilter jwtFilter) {
        this.jwtFilter = jwtFilter;
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();//密码加密算法类
    }

    @Bean
    public AuthenticationProvider authenticationProvider() {
        DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
        provider.setPasswordEncoder(passwordEncoder());  // 密码加密
        provider.setUserDetailsService(resuserBizImpl);  // 组装一个业务
        return provider;
    }

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration configuration) throws Exception {
        return configuration.getAuthenticationManager();
    }

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity httpSecurity) throws Exception {

        httpSecurity.formLogin(AbstractHttpConfigurer::disable)  // 关闭默认的登录
                .logout(AbstractHttpConfigurer::disable)  // 关闭默认的登出
                .authenticationProvider(authenticationProvider())  // 配置一个业务
                .csrf(AbstractHttpConfigurer::disable)  // 关闭默认的csrf
                .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))  // 关闭默认的session
                .httpBasic(AbstractHttpConfigurer::disable) // 关闭默认的httpBasic认证
                .authorizeHttpRequests(request ->
                        request.requestMatchers(HttpMethod.POST,"/ressecurity/login","/ressecurity/register", "/ressecurity/logout", "/ressecurity/getTelValcode", "/ressecurity/loginByTel").permitAll()
                                .requestMatchers(HttpMethod.GET, "/ressecurity/captcha").permitAll()
                                .anyRequest().authenticated()
                        )
                .addFilterBefore(jwtFilter, UsernamePasswordAuthenticationFilter.class);

        return httpSecurity.build();
    }
}
